home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / usenet / volume3 / puzzle < prev    next >
Encoding:
Internet Message Format  |  1988-02-19  |  16.0 KB

  1. Path: uunet!husc6!mailrus!ames!ll-xn!mit-eddie!uw-beaver!tektronix!tekgen!tekred!games-request
  2. From: games-request@tekred.TEK.COM
  3. Newsgroups: comp.sources.games
  4. Subject: v03i083:  puzzle - move the pieces through the hole
  5. Message-ID: <2193@tekred.TEK.COM>
  6. Date: 19 Feb 88 00:58:53 GMT
  7. Sender: billr@tekred.TEK.COM
  8. Lines: 605
  9. Approved: billr@tekred.TEK.COM
  10.  
  11. Submitted by: Henk <mcvax!cs.vu.nl!Henk@uunet.uu.net>
  12. Comp.sources.games: Volume 3, Issue 83
  13. Archive-name: puzzle
  14.  
  15.     [Note that this program requires some non-standard(?) termcap
  16.      entries (hi, he) for highlighting (in addition to so & se),
  17.      as well as ho (home). Since my terminal termcaps don't support
  18.      hi & he I couldn't try this program, although I did compile it
  19.      OK.    -br]
  20.  
  21. #! /bin/sh
  22. # This is a shell archive.  Remove anything before this line, then unpack
  23. # it by saving it into a file and typing "sh file".  To overwrite existing
  24. # files, type "sh file -c".  You can also feed this as standard input via
  25. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  26. # will see the following message at the end:
  27. #        "End of shell archive."
  28. # Contents:  README Makefile puzzle.c
  29. # Wrapped by billr@saab on Thu Feb 18 16:47:07 1988
  30. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  31. if test -f README -a "${1}" != "-c" ; then 
  32.   echo shar: Will not over-write existing file \"README\"
  33. else
  34. echo shar: Extracting \"README\" \(535 characters\)
  35. sed "s/^X//" >README <<'END_OF_README'
  36. XPUZZLE   (By HJ Visscher)
  37. X
  38. XPurpose  :
  39. XTry to move the biggest piece through the hole at the top.
  40. X
  41. XCommands  :
  42. Xy   k   u  \\
  43. X  \\ | /     \\
  44. Xh - * - l    |- To Move
  45. X  / | \\     /
  46. Xb   j   n  /
  47. X
  48. XQ - Quit
  49. XT - Take back
  50. XR - Redraw
  51. X? - Solution
  52. XN - New Game
  53. XA - Extra accent
  54. X
  55. XThe solution I included is not the perfect solution. That solution 
  56. Xonly requires 116 moves (instead of 152), but while writing this program
  57. XI didn't know this solution. Anyone intrested in this solution can mail
  58. Xme for it. 
  59. X
  60. XHenk
  61. Xmcvax!cs.vu.nl!Henk@uunet.uu.net
  62. END_OF_README
  63. if test 535 -ne `wc -c <README`; then
  64.     echo shar: \"README\" unpacked with wrong size!
  65. fi
  66. # end of overwriting check
  67. fi
  68. if test -f Makefile -a "${1}" != "-c" ; then 
  69.   echo shar: Will not over-write existing file \"Makefile\"
  70. else
  71. echo shar: Extracting \"Makefile\" \(82 characters\)
  72. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  73. X# Simple makefile for puzzle
  74. Xpuzzle: puzzle.c
  75. X    cc -o puzzle -O puzzle.c -ltermcap
  76. END_OF_Makefile
  77. if test 82 -ne `wc -c <Makefile`; then
  78.     echo shar: \"Makefile\" unpacked with wrong size!
  79. fi
  80. # end of overwriting check
  81. fi
  82. if test -f puzzle.c -a "${1}" != "-c" ; then 
  83.   echo shar: Will not over-write existing file \"puzzle.c\"
  84. else
  85. echo shar: Extracting \"puzzle.c\" \(12931 characters\)
  86. sed "s/^X//" >puzzle.c <<'END_OF_puzzle.c'
  87. X    /*
  88. X     *    Puzzle,  By Henk-Jan Visscher.
  89. X     * 
  90. X     *    compile:
  91. X     *        cc -O puzzle.c -o puzzle -ltermcap
  92. X     *        strip puzzle
  93. X     *
  94. X     *    NB : change UID in own UID.
  95. X     */
  96. X
  97. X#include    <stdio.h>
  98. X#include    <signal.h>
  99. X#include    <sys/types.h>
  100. X#include    <sgtty.h>
  101. X#include    <pwd.h>
  102. X
  103. Xextern    char    *tgoto ();
  104. X
  105. X#define        scr_init()    tputs (IS, 0, xputc)
  106. X#define        clear()        tputs (CL, 0, xputc)
  107. X#define     rev_begin()    tputs (SO, 0, xputc)
  108. X#define     rev_end()    tputs (SE, 0, xputc)
  109. X#define        cursor(y, x)    tputs (tgoto (CM, x, y), 0, xputc)
  110. X#define        hi_begin()    tputs (HI, 0, xputc)
  111. X#define        hi_end()    tputs (HE, 0, xputc)
  112. X#define        home()        tputs (HO, 0, xputc)
  113. X#define        bell()        xputc ('\007')
  114. X#define        flush()        fflush (stdout)
  115. X
  116. X#define        UID        320
  117. X
  118. X#define        BORDER        -1
  119. X#define        EMPTY        0
  120. X#define        SMALL        1
  121. X#define        WIDTH        2
  122. X#define        HIGH        3
  123. X#define        BIG        4
  124. X
  125. X#define        CLEAR        0
  126. X#define        DRAW        1
  127. X#define        ACCENT        2
  128. X
  129. Xchar    *CL, *SO, *SE, *IS, *CM, *HI, *HE, *HO, ch;
  130. Xshort    x, y;
  131. Xint    moves;
  132. X
  133. Xstruct    sgttyb    sg, save;
  134. X
  135. Xstruct field {
  136. X    short    hx, hy, type;
  137. X} field [6] [7];
  138. X
  139. Xstruct mem {
  140. X    short   nx, ny, ox, oy;
  141. X} tb [9999];
  142. X
  143. Xstruct mem sol [] = {
  144. X    { 1, 1, 2, 1 } , { 1, 2, 1, 1 } , { 2, 3, 1, 3 } , { 3, 2, 3, 1 } ,
  145. X    { 4, 2, 3, 2 } , { 4, 1, 4, 2 } , { 3, 1, 4, 1 } , { 3, 2, 3, 1 } ,
  146. X    { 1, 3, 2, 3 } , { 2, 3, 3, 3 } , { 2, 2, 2, 3 } , { 2, 1, 2, 2 } , 
  147. X    { 2, 3, 1, 3 } , { 2, 2, 2, 3 } , { 3, 1, 2, 1 } , { 4, 2, 3, 2 } ,
  148. X    { 3, 2, 3, 1 } , { 3, 3, 3, 2 } , { 2, 3, 3, 3 } , { 3, 3, 4, 3 } ,
  149. X    { 1, 3, 2, 3 } , { 2, 3, 3, 3 } , { 1, 1, 1, 2 } , { 2, 1, 2, 2 } ,
  150. X    { 3, 1, 2, 1 } , { 2, 1, 1, 1 } , { 4, 1, 3, 1 } , { 3, 1, 2, 1 } ,
  151. X    { 3, 2, 3, 1 } , { 3, 3, 3, 2 } , { 3, 2, 4, 2 } , { 2, 2, 3, 2 } ,
  152. X    { 1, 2, 2, 2 } , { 1, 4, 1, 3 } , { 1, 3, 1, 2 } , { 2, 4, 1, 4 } ,
  153. X    { 4, 4, 3, 4 } , { 4, 3, 4, 4 } , { 4, 4, 4, 5 } , { 4, 2, 4, 3 } ,
  154. X    { 4, 3, 4, 4 } , { 3, 2, 4, 2 } , { 3, 4, 3, 3 } , { 3, 3, 3, 2 } ,
  155. X    { 1, 4, 2, 4 } , { 1, 2, 1, 3 } , { 1, 3, 1, 4 } , { 2, 2, 1, 2 } ,
  156. X    { 3, 2, 2, 2 } , { 4, 2, 3, 2 } , { 4, 4, 4, 3 } , { 4, 3, 4, 2 } ,
  157. X    { 4, 5, 4, 4 } , { 4, 4, 4, 3 } , { 2, 4, 3, 4 } , { 2, 2, 2, 3 } ,
  158. X    { 2, 3, 2, 4 } , { 3, 2, 2, 2 } , { 4, 2, 3, 2 } , { 3, 2, 3, 3 } ,
  159. X    { 3, 1, 3, 2 } , { 2, 1, 3, 1 } , { 3, 1, 4, 1 } , { 1, 1, 2, 1 } , 
  160. X    { 2, 1, 3, 1 } , { 1, 2, 1, 1 } , { 2, 2, 2, 1 } , { 3, 3, 2, 3 } ,
  161. X    { 2, 3, 1, 3 } , { 4, 3, 3, 3 } , { 3, 3, 2, 3 } , { 3, 2, 3, 3 } ,
  162. X    { 4, 1, 4, 2 } , { 3, 1, 4, 1 } , { 2, 1, 3, 1 } , { 2, 3, 2, 2 } , 
  163. X    { 2, 2, 2, 1 } , { 2, 4, 2, 3 } , { 2, 3, 2, 2 } , { 1, 4, 2, 4 } ,
  164. X    { 1, 3, 1, 4 } , { 1, 4, 1, 5 } , { 1, 1, 1, 2 } , { 1, 2, 1, 3 } ,
  165. X    { 2, 1, 1, 1 } , { 1, 1, 1, 2 } , { 2, 2, 2, 1 } , { 2, 4, 2, 3 } ,
  166. X    { 1, 5, 2, 5 } , { 1, 3, 1, 4 } , { 1, 2, 1, 3 } , { 2, 1, 1, 1 } ,
  167. X    { 2, 3, 2, 2 } , { 2, 2, 2, 1 } , { 1, 3, 2, 3 } , { 2, 3, 2, 4 } ,
  168. X    { 1, 4, 1, 3 } , { 2, 5, 1, 5 } , { 2, 4, 2, 5 } , { 2, 1, 2, 2 } ,
  169. X    { 2, 2, 2, 3 } , { 3, 1, 2, 1 } , { 4, 1, 3, 1 } , { 4, 2, 4, 1 } , 
  170. X    { 3, 3, 3, 2 } , { 3, 4, 3, 3 } , { 2, 5, 3, 5 } , { 3, 5, 4, 5 } ,
  171. X    { 1, 5, 2, 5 } , { 2, 5, 3, 5 } , { 1, 3, 1, 4 } , { 1, 1, 1, 2 } ,
  172. X    { 2, 3, 2, 4 } , { 2, 1, 2, 2 } , { 3, 1, 2, 1 } , { 2, 1, 1, 1 } ,
  173. X    { 4, 1, 3, 1 } , { 3, 1, 2, 1 } , { 3, 2, 3, 1 } , { 3, 3, 3, 2 } ,
  174. X    { 3, 5, 3, 4 } , { 3, 4, 4, 4 } , { 2, 4, 3, 4 } , { 2, 2, 2, 3 } ,
  175. X    { 2, 3, 2, 4 } , { 3, 2, 2, 2 } , { 4, 4, 4, 3 } , { 4, 3, 4, 2 } ,
  176. X    { 4, 5, 4, 4 } , { 4, 4, 4, 3 } , { 3, 4, 4, 4 } , { 2, 4, 3, 4 } ,
  177. X    { 1, 4, 2, 4 } , { 1, 2, 1, 3 } , { 1, 3, 1, 4 } , { 2, 2, 1, 2 } ,
  178. X    { 4, 2, 3, 2 } , { 3, 2, 3, 3 } , { 3, 1, 3, 2 } , { 2, 1, 3, 1 } ,
  179. X    { 3, 1, 4, 1 } , { 1, 1, 2, 1 } , { 2, 1, 3, 1 } , { 1, 2, 1, 1 } , 
  180. X    { 3, 3, 2, 3 } , { 2, 3, 1, 3 } , { 4, 3, 3, 3 } , { 3, 3, 2, 3 } ,
  181. X    { 3, 2, 3, 3 } , { 3, 1, 3, 2 } , { 3, 2, 4, 2 } , { 1, 1, 2, 1 } ,
  182. X    { 0, 0, 0, 0 }
  183. X};
  184. X
  185. Xmain ()
  186. X{
  187. X    init ();
  188. X    layout ();
  189. X    initfield (); 
  190. X    for (;;)
  191. X        move ();
  192. X}
  193. X
  194. Xinit ()
  195. X{
  196. X    int    leave ();
  197. X#ifdef    SIGTSTP
  198. X    int    sigtstp();
  199. X#endif    SIGTSTP
  200. X    int    xputc ();
  201. X    char    buffer [1024];
  202. X    static  char    save_buf [100];
  203. X    char    *ptr  = save_buf;
  204. X    extern     char    *tgetstr ();
  205. X
  206. X    signal (SIGINT, leave);
  207. X    signal (SIGQUIT, leave);
  208. X#ifdef    SIGTSTP
  209. X    signal (SIGTSTP, sigtstp);
  210. X#endif  SIGTSTP
  211. X
  212. X    gtty (1, &sg);
  213. X    save = sg;
  214. X    sg.sg_flags |= CBREAK;
  215. X    sg.sg_flags &= ~ECHO;
  216. X    stty (1, &sg);
  217. X
  218. X    switch (tgetent (buffer, getenv ("TERM"))) {
  219. X        case -1:
  220. X            fprintf (stderr, "can't open \"termcap\" file.\n");
  221. X            exit (1);
  222. X        case 0:
  223. X            fprintf (stderr, "can't find entry for term.type.\n");
  224. X            exit (1);
  225. X    }
  226. X
  227. X    IS = tgetstr ("is", &ptr);
  228. X    CL = tgetstr ("cl", &ptr);
  229. X    CM = tgetstr ("cm", &ptr);
  230. X    SO = tgetstr ("so", &ptr);
  231. X    SE = tgetstr ("se", &ptr);
  232. X    HI = tgetstr ("hi", &ptr);
  233. X    HE = tgetstr ("he", &ptr);
  234. X    HO = tgetstr ("ho", &ptr);
  235. X    if (!CL || !CM || !SO || !SE || !HI || !HE || !HO) {
  236. X        fprintf (stderr, "not a right terminal.\n");
  237. X        stty (1, &save);
  238. X        exit (1);
  239. X    }
  240. X    if (IS != (char *) 0)    scr_init ();
  241. X    ch = ' ';
  242. X}
  243. X
  244. X#ifdef    SIGTSTP
  245. Xsigtstp()
  246. X{
  247. X    signal (SIGTSTP, SIG_IGN);
  248. X    stty (1, &save);
  249. X    cursor (23, 0);
  250. X    flush ();
  251. X    kill (0, SIGSTOP);
  252. X    signal (SIGTSTP, sigtstp);
  253. X    stty (1, &sg);
  254. X    redraw ();
  255. X}
  256. X#endif    SIGTSTP
  257. X
  258. Xleave ()
  259. X{
  260. X    stty (1, &save);
  261. X    cursor (23, 0);
  262. X    exit (0);
  263. X}
  264. X
  265. Xfill (x, y, px, py, mode)
  266. Xshort    x, y, px, py, mode;
  267. X{
  268. X    short    i, j;
  269. X
  270. X    for (i = y; i < y+py; i++)
  271. X    {
  272. X        cursor (i, x);
  273. X        if (mode != CLEAR) rev_begin ();
  274. X        if (mode == ACCENT) hi_begin ();
  275. X        for (j = x; j < x+px; j++)
  276. X            putchar ((mode == ACCENT) ? ch : ' ');
  277. X        if (mode != CLEAR) rev_end ();
  278. X        if (mode == ACCENT) hi_end ();
  279. X    }
  280. X}
  281. X
  282. X/* VARARGS3 */
  283. Xpline (yc, xc, format, arg)
  284. Xshort    yc, xc;
  285. Xchar    *format;
  286. Xint    arg;
  287. X{
  288. X    cursor (yc, xc);
  289. X    printf (format, arg);
  290. X}
  291. X
  292. Xlayout ()
  293. X{
  294. X    clear ();
  295. X    fill (7, 2, 34, 20, DRAW);
  296. X    fill (18, 2, 12, 2, CLEAR);
  297. X    fill (11, 4, 26, 16, CLEAR);
  298. X    pline (3, 46, "PUZZLE   (By HJ Visscher)");
  299. X    pline (5, 46, "Purpose  :");
  300. X    pline (6, 46, "----------");
  301. X    pline (7, 46, "Try to move the biggest");
  302. X    pline (8, 46, "piece through the hole at");
  303. X    pline (9, 46, "the top.");
  304. X    pline (11, 46, "Commands  :");
  305. X    pline (12, 46, "------------");
  306. X    pline (13, 46, "y   k   u  \\");
  307. X    pline (14, 46, "  \\ | /     \\");
  308. X    pline (15, 46, "h - * - l    |- To Move");
  309. X    pline (16, 46, "  / | \\     /");
  310. X    pline (17, 46, "b   j   n  /");
  311. X    pline (19, 46, "Q - Quit      T - Take back");
  312. X    pline (20, 46, "R - Redraw    ? - Solution");
  313. X    pline (21, 46, "N - New Game  A - Extra accent");
  314. X}
  315. X
  316. Xredraw ()
  317. X{
  318. X    short    i, j;
  319. X
  320. X    layout ();
  321. X    for (i = 1; i < 5; i++)
  322. X        for (j = 1; j < 6; j++)
  323. X            if (field [i] [j]. hx == i && field [i] [j]. hy == j)
  324. X                change (DRAW, i, j, field [i] [j]. type);
  325. X    change (ACCENT, x, y, field [x] [y]. type);
  326. X    pline (22, 20, "Moves : %04d", moves);
  327. X    home ();
  328. X}
  329. X
  330. Xclfield ()
  331. X{
  332. X    short i, j;
  333. X    static struct field empty_field;
  334. X
  335. X    for (i = 0; i < 6; i++)
  336. X        for (j = 0; j < 7; j++)
  337. X            field [i] [j] = empty_field;
  338. X    for (i = 0; i < 6; i++)
  339. X        field [i] [0]. type = field [i] [6]. type = BORDER;
  340. X    for (j = 0; j < 7; j++)
  341. X        field [0] [j]. type = field [5] [j]. type = BORDER;
  342. X}
  343. X
  344. Xchange (task, xc, yc, kind)
  345. Xshort    task, xc, yc, kind;
  346. X{
  347. X    field [xc] [yc]. hx = (task == CLEAR) ? 0 : xc;
  348. X    field [xc] [yc]. hy = (task == CLEAR) ? 0 : yc;
  349. X    field [xc] [yc]. type = (task == CLEAR) ? EMPTY : kind;
  350. X    switch (kind) {
  351. X        case SMALL:
  352. X            fill (7+xc*6, 2+yc*3, 4, 2, task);
  353. X            return;
  354. X        case WIDTH:
  355. X            field [xc+1] [yc]. hx = (task == CLEAR) ? 0 : xc;
  356. X            field [xc+1] [yc]. hy = (task == CLEAR) ? 0 : yc;
  357. X            fill (7+xc*6, 2+yc*3, 10, 2, task);
  358. X            return;
  359. X        case HIGH:
  360. X            field [xc] [yc+1]. hx = (task == CLEAR) ? 0 : xc;
  361. X            field [xc] [yc+1]. hy = (task == CLEAR) ? 0 : yc;
  362. X            fill (7+xc*6, 2+yc*3, 4, 5, task);
  363. X            return;
  364. X        case BIG:
  365. X            field [xc+1] [yc]. hx = field [xc] [yc+1]. hx =
  366. X                field [xc+1] [yc+1]. hx = 
  367. X                (task == CLEAR) ? 0 : xc;
  368. X            field [xc+1] [yc]. hy = field [xc] [yc+1]. hy =
  369. X                field [xc+1] [yc+1]. hy = 
  370. X                (task == CLEAR) ? 0 : yc;
  371. X            fill (7+xc*6, 2+yc*3, 10, 5, task);
  372. X            return;
  373. X    }
  374. X}
  375. X
  376. Xinitfield ()
  377. X{
  378. X    clfield ();
  379. X    change (ACCENT, 1, 1, SMALL);
  380. X    change (DRAW, 2, 2, SMALL);
  381. X    change (DRAW, 3, 2, SMALL);
  382. X    change (DRAW, 4, 1, SMALL);
  383. X    change (DRAW, 1, 2, HIGH);
  384. X    change (DRAW, 1, 4, HIGH);
  385. X    change (DRAW, 4, 2, HIGH);
  386. X    change (DRAW, 4, 4, HIGH);
  387. X    change (DRAW, 2, 3, WIDTH);
  388. X    change (DRAW, 2, 4, BIG);
  389. X    x = y = 1;
  390. X    moves = 0;
  391. X    pline (22, 20, "Moves : %04d", moves);
  392. X    home ();
  393. X}
  394. X
  395. Xmove ()
  396. X{
  397. X    char    c;
  398. X    short    nx, ny, px, py;
  399. X    short    movem, kind;
  400. X
  401. X    c = getchar ();
  402. X
  403. X    px = (field [x] [y]. type == BIG||field [x] [y]. type == WIDTH) ? 2 : 1;
  404. X    py = (field [x] [y]. type == BIG||field [x] [y]. type == HIGH) ? 2 : 1;
  405. X    movem = 0;
  406. X    switch (c) {
  407. X        case 'A' : ch = (ch == ' ') ? ':' : ' ';
  408. X               change (ACCENT, x, y, field [x] [y]. type);
  409. X               home ();
  410. X               return;
  411. X        case 'Q' : clear ();
  412. X               leave ();
  413. X        case 'R' : redraw ();
  414. X               return;
  415. X        case '?' : solution ();
  416. X               return;
  417. X        case 'T' : takeback ();
  418. X               return;
  419. X        case 'N' : new ();
  420. X               return;
  421. X        case 'h' : nx = field [x-1] [y]. hx;
  422. X               ny = field [x-1] [y]. hy;
  423. X               if (nx == EMPTY && py == 2) {
  424. X                nx = field [x-1] [y+1]. hx;
  425. X                ny = field [x-1] [y+1]. hy;
  426. X               }
  427. X               movem = 1;
  428. X               break;
  429. X        case 'j' : nx = field [x] [y+py]. hx;
  430. X               ny = field [x] [y+py]. hy;
  431. X               if (nx == EMPTY && px == 2) {
  432. X                nx = field [x+1] [y+py]. hx;
  433. X                ny = field [x+1] [y+py]. hy;
  434. X               }
  435. X               movem = 1;
  436. X               break;
  437. X        case 'k' : nx = field [x] [y-1]. hx;
  438. X               ny = field [x] [y-1]. hy;
  439. X               if (nx == EMPTY && px == 2) {
  440. X                nx = field [x+1] [y-1]. hx;
  441. X                ny = field [x+1] [y-1]. hy;
  442. X               }
  443. X               movem = 1;
  444. X               break;
  445. X        case 'l' : nx = field [x+px] [y]. hx;
  446. X               ny = field [x+px] [y]. hy;
  447. X               if (nx == EMPTY && py == 2) {
  448. X                nx = field [x+px] [y+1]. hx;
  449. X                ny = field [x+px] [y+1]. hy;
  450. X               }
  451. X               movem = 1;
  452. X               break;
  453. X        case 'y' : nx = field [x-1] [y-1]. hx;
  454. X               ny = field [x-1] [y-1]. hy;
  455. X               break;
  456. X        case 'u' : nx = field [x+px] [y-1]. hx;
  457. X               ny = field [x+px] [y-1]. hy;
  458. X               if (nx == EMPTY) {
  459. X                nx = field [x+1] [y-1]. hx;
  460. X                   ny = field [x+1] [y-1]. hy;
  461. X               }
  462. X               break;
  463. X        case 'b' : nx = field [x-1] [y+py]. hx;
  464. X               ny = field [x-1] [y+py]. hy;
  465. X               if (nx == EMPTY) {
  466. X                nx = field [x-1] [y+1]. hx;
  467. X                   ny = field [x-1] [y+1]. hy;
  468. X               }
  469. X               break;
  470. X        case 'n' : nx = field [x+px] [y+py]. hx;
  471. X               ny = field [x+px] [y+py]. hy;
  472. X               if (nx == EMPTY) {
  473. X                nx = field [x+1] [y+1]. hx;
  474. X                   ny = field [x+1] [y+1]. hy;
  475. X               }
  476. X               break;
  477. X        default  : bell ();
  478. X               return;
  479. X    }
  480. X
  481. X    if (nx != EMPTY) {
  482. X        change (DRAW, x, y, field [x] [y]. type);
  483. X        change (ACCENT, nx, ny, field [nx] [ny]. type);
  484. X        home ();
  485. X        x = nx; y = ny;
  486. X        return;
  487. X    }
  488. X
  489. X    if (!movem) {
  490. X        bell ();
  491. X        return;
  492. X    }
  493. X
  494. X    kind = field [x] [y]. type;
  495. X    change (CLEAR, x, y, kind);
  496. X    nx = x + (c == 'l') - (c == 'h');
  497. X    ny = y + (c == 'j') - (c == 'k');
  498. X    if (!space (nx, ny, kind)) {
  499. X        if (c == 'k' && kind == BIG && x == 2 && y == 1) solved ();
  500. X        change (ACCENT, x, y, kind);
  501. X        bell ();
  502. X        home ();
  503. X        return;
  504. X    }
  505. X    change (ACCENT, nx, ny, kind);
  506. X    pline (22, 20, "Moves : %04d", ++moves);
  507. X    home ();
  508. X    tb [moves]. ox = x; 
  509. X    tb [moves]. oy = y;
  510. X    tb [moves]. nx = nx; 
  511. X    tb [moves]. ny = ny;
  512. X    x = nx; y = ny;
  513. X}
  514. X
  515. Xspace (xc, yc, kind)
  516. Xshort     xc, yc, kind;
  517. X{
  518. X    if (field [xc] [yc]. type) return (0);
  519. X    switch (kind) {
  520. X        case SMALL : return (1);
  521. X        case WIDTH : return (field [xc+1] [yc]. type == EMPTY);
  522. X        case HIGH  : return (field [xc] [yc+1]. type == EMPTY);
  523. X        case BIG : return (field [xc+1] [yc]. type == EMPTY && 
  524. X                     field [xc] [yc+1]. type == EMPTY &&
  525. X                     field [xc+1] [yc+1]. type == EMPTY);
  526. X    }
  527. X    /* NOTREACHED */
  528. X}
  529. X
  530. Xsolved ()
  531. X{
  532. X    fill (11, 4, 26, 16, CLEAR);
  533. X    fill (15, 10, 18, 5, ACCENT);
  534. X    fill (17, 11, 14, 3, DRAW);
  535. X    pline (12, 19, "SOLVED !!!");
  536. X    leave ();
  537. X}
  538. X
  539. Xxputc (c)
  540. Xchar    c;
  541. X{
  542. X    putchar (c);
  543. X}
  544. X
  545. Xsolution ()
  546. X{
  547. X    struct    passwd    *pw, *getpwuid();
  548. X    short    i, kind;
  549. X    char    *crypt ();
  550. X
  551. X    if ((pw = getpwuid (UID)) == (struct passwd *) 0) {
  552. X        pline (22, 20, "Sorry....       ");
  553. X        home ();
  554. X        return;
  555. X    }
  556. X    cursor (22, 20);
  557. X    printf ("Password:   ");
  558. X    flush ();
  559. X    if (strcmp (crypt (getpass(""), pw->pw_passwd), pw->pw_passwd)) {
  560. X        pline (22, 20, "Sorry....       ");
  561. X        home ();
  562. X        return;
  563. X    }
  564. X    new ();
  565. X    pline (22, 20, "Moves : %04d         ", moves);
  566. X    for (i = 0; sol [i]. nx; i++) {
  567. X        kind = field [sol [i]. nx] [sol [i]. ny]. type;
  568. X        change (CLEAR, sol [i]. nx, sol [i]. ny, kind);
  569. X        change (DRAW, (x = sol [i]. ox), (y = sol [i]. oy), kind);
  570. X        pline (22, 28, "%04d", ++moves);
  571. X        home ();
  572. X    }
  573. X    solved ();
  574. X}
  575. X
  576. Xtakeback ()
  577. X{
  578. X    int    count;
  579. X    short    i, kind;
  580. X    char    buffer [BUFSIZ];
  581. X
  582. X    pline (22, 15, "How many moves ? ");
  583. X    stty (1, &save);
  584. X    gets (buffer);
  585. X    count = atoi (buffer);
  586. X    stty (1, &sg);
  587. X    if (moves - count < 0) {
  588. X        pline (22, 15, "     Sorry         ");
  589. X        home ();
  590. X        return;
  591. X    }
  592. X    pline (22, 15, "                   ");
  593. X    for (i = 0; i < count; i++) {
  594. X        kind = field [tb [moves]. nx] [tb [moves]. ny]. type;
  595. X        change (DRAW, x, y, field [x] [y]. type);
  596. X        change (CLEAR, tb [moves]. nx, tb [moves]. ny, kind);
  597. X        change (ACCENT, (x = tb [moves]. ox), (y = tb [moves]. oy), kind);
  598. X        pline (22, 20, "Moves : %04d", --moves);
  599. X        home ();
  600. X    }
  601. X}
  602. X
  603. Xnew ()
  604. X{
  605. X    fill (11, 4, 26, 16, CLEAR);
  606. X    initfield ();
  607. X}
  608. END_OF_puzzle.c
  609. if test 12931 -ne `wc -c <puzzle.c`; then
  610.     echo shar: \"puzzle.c\" unpacked with wrong size!
  611. fi
  612. # end of overwriting check
  613. fi
  614. echo shar: End of shell archive.
  615. exit 0
  616.